<?php
/**
 * Обработчик для типа документа комментарий
 * ид комментария, ид комментируемого итема, ид родительского комментария, автор комментария, дата добавления, текст, урл
 * @package eventsfetcher
 * @author Alexander Sibiryakov
 **/
class CommentEventHandler extends EventHandler 
{	
	private $oid;
	private $item_oid;
	private $parent_oid;
	
	private $author_openid;
	private $create_dt;
	private $edited_dt;	
	private $text;
	private $url;
	
	// данные (ид итема, автор, дата добавления, урл, название, анонс, текст) -- каждое поле в отдельной ноде
	public function fetchData() {
		$sql = "SELECT * FROM store_objects WHERE store_objects.oid = ".$this->event->oid;
		$result = $this->sm_db->query($sql);
		if ($result) {
			$row = mysql_fetch_assoc($result);
			mysql_free_result($result);
			$hash = unserialize($row['data']);

			$sql = "SELECT tat2.id_object AS parent_oid FROM t_acl_tree 
			INNER JOIN t_acl_tree tat2 ON tat2.id = t_acl_tree.parent_id
			WHERE t_acl_tree.id_object=".$this->event->oid;
			$rs = $this->sm_db->query($sql);
			if ($rs) {
				$row2 = mysql_fetch_assoc($rs);
				mysql_free_result($rs);
			}

			$this->oid = $this->event->oid;
			
			if ($hash['item_oid'] != $row2['parent_oid'])
				$this->parent_oid = $row2['parent_oid'];
			
				
			$this->item_oid = $hash['item_oid'];
			
			$this->author_openid = $this->event->openid;
			$this->create_dt =date($this->date_format, RL_CyrDate::newFromStr("%Y-%m-%d %H:%i:%s", $row['ctime'])->unix_mark());
			$this->edited_dt = date($this->date_format, RL_CyrDate::newFromStr("%Y-%m-%d %H:%i:%s", $row['mtime'])->unix_mark());			
			
			$this->text = $hash['text'];
			$this->url = $this->url_gen->createCommentGen()->genUrl(array($hash['item_oid'], $this->event->oid));
			
		}
	}
	
	public function createElements(DOMDocument $dom ) {

		$chlds = array();
		$chlds[] = $dom->createElement("id", $this->oid);
		$chlds[] = $dom->createElement("author_openid_url", $this->esc($this->author_openid));
		$chlds[] = $dom->createElement("create_datetime", $this->create_dt);
		$chlds[] = $dom->createElement("edit_datetime", $this->edited_dt);		
		$chlds[] = $dom->createElement("url", $this->esc($this->url));
		$chlds[] = $dom->createElement("summary", $this->esc($this->text));
		$chlds[] = $dom->createElement("parent_post_id", $this->item_oid);
		$chlds[] = $dom->createElement("is_comment", 1);
		
		if ($this->parent_oid)
			$chlds[] = $dom->createElement("parent_comment_id", $this->parent_oid);
			
		return $chlds;
	}
	
} // END class 